package com.aifast.core.db.handler;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.schema.Column;

import java.util.List;

/**
 * @Author fujian
 * @since 2022/10/30 16:58
 */
public class AiTenantHandler implements TenantLineHandler {
    private static final String COLUMN = "tenant_id";

    @Override
    public Expression getTenantId() {
        /*if (!StpUtil.isLogin()){
            return new LongValue(-1);
        }*/
        Integer tenantId = Convert.toInt(StpUtil.getSession().get("tenantId"));
        return new LongValue(Opt.ofNullable(tenantId).orElse(-1));
    }

    @Override
    public String getTenantIdColumn() {
        return COLUMN;
    }

    @Override
    public boolean ignoreTable(String tableName) {
        if(StrUtil.contains(tableName, "gen_")
                || StrUtil.contains(tableName, "fd_")
                || StrUtil.contains(tableName, "tenant")
                || StrUtil.contains(tableName, "information_schema")
            ){
            return true;
        }
        return false;
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return false;
    }
}
